package com.ie.sevice.admin;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.MD5Util;
import com.ie.common.security.utils.RedisUtil;
import com.ie.dto.PasswordDTO;
import com.ie.entity.AUserEntity;
import com.ie.mapper.admin.AUserMapper;
import com.ie.sevice.admin.AUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

@Service
public class AUserServiceImpl extends ServiceImpl<AUserMapper, AUserEntity> implements AUserService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;
    // 对传入的对象参数进行比较
    // 1.有这个用户:a.密码正确;b.密码错误
    // 无此用户:返回
    // 2.密码正确:返回token,跳转到相应展示页面
    @Override
    public ResponseResult<Map<String, String>> login(AUserEntity aUserEntity) {
// 获取到这个参数
        Long count = lambdaQuery().eq(AUserEntity::getAname, aUserEntity.getAname()).count();
        if (count == 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        HashMap<String, String> mapuser = new HashMap<>();
        mapuser.put("aname", aUserEntity.getAname());
        mapuser.put("apwd", aUserEntity.getApwd());
        List<AUserEntity> userlist = query().allEq(mapuser).list();
        if (userlist.size() > 0) {
            String token = jwtTokenUtil.createToken(aUserEntity.getAname());
            redisUtil.set("login_" + aUserEntity.getAname(), aUserEntity.getAname(), config.getRedisExpiration());
            // 把生成的有效信息返回给ResponseResultl
            HashMap<String, String> loginresult = new HashMap<>();
            loginresult.put("authtoken", token);
            loginresult.put("aname", aUserEntity.getAname());
            loginresult.put("aid", aUserEntity.getId() + "");
            return ResponseResult.getSuccessResult(loginresult);
        } else {
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Object> updatePassword(PasswordDTO passwordDTO) {
        // 1. 根据id查询用户
        AUserEntity aUserEntity = this.getById(passwordDTO.getId());
        // 2. 检查用户是否存在
        if (aUserEntity == null) {
            return ResponseResult.getMessageResult(null, "A003"); // A003:不存在
        }
        // 3. 验证旧密码是否正确 (加密后比较)
        if (!aUserEntity.getApwd().equals(MD5Util.MD5(passwordDTO.getOldPassword()))) {
            return ResponseResult.getMessageResult(null, "A002"); // A002: 密码错误
        }
        // 4. 更新新密码 (加密后存储)
        aUserEntity.setApwd(MD5Util.MD5(passwordDTO.getNewPassword()));
        boolean updated = this.updateById(aUserEntity);

        if (updated) {
            return ResponseResult.getMessageResult(true, "密码修改成功");
        } else {
            return ResponseResult.getMessageResult(false, "A001"); // A001: 失败
        }
    }
}
